package com.xcity.game.peakroad;

import java.util.List;

import com.xcity.db.entity.Player;
import com.xcity.game.combat.attr.CombatAttribute;
import com.xcity.game.common.GameStaticConfig;
import com.xcity.game.peakroad.model.PeakRoadInstance;
import com.xcity.game.peakroad.template.PeakRoadTemplate;

import naga.x.App;
import naga.x.game.time.TimeUpdater;

public class PeakRoadUtils {

	static PeakRoadService service;
	
	static PeakRoadService ser() {
		return service != null ? service : (service = App.getApp().getServiceManager().get(PeakRoadService.class));
	}
	
	public static int getTopFloor() {
		return ser().getTemplates().size();
	}
	
	public static List<PeakRoadTemplate> getTemplates() {
		return ser().getTemplates();
	}
	
	public static PeakRoadTemplate getTemplate(int floor) {
		int i = floor - 1;
		List<PeakRoadTemplate> list = ser().getTemplates();
		return i >= 0 && i < list.size() ? list.get(i) : null;
	}
	
	public static int getMaxTimes() {
		return GameStaticConfig.peakRoadTimes;
	}
	
	public static int getLeftTimes(Player player) {
		PeakRoadInstance instance = player.getPeakRoadInstance();
		if (instance.getLastChallengeDay() != TimeUpdater.getInstance().today()) {
			return GameStaticConfig.peakRoadTimes;
		}
		int n = GameStaticConfig.peakRoadTimes - instance.getJoinedTimes();
		return n > 0 ? n : 0;
	}
	
	public static void addChallengeTimes(Player player, int n) {
		PeakRoadInstance instance = player.getPeakRoadInstance();
		if (!instance.hasState(PeakRoadInstance.STATE_PREVIOUS_CHALLENGE_MASK)) { // 上一次挑战失败才计算次数
			instance.setJoinedTimes(instance.getJoinedTimes() + n);
		}
	}
	
	public static float addBuff(float oldVal, CombatAttribute buff) {
		return oldVal + oldVal * buff.getValue() / GameStaticConfig.percentBase;
	}
	
	public static boolean isSweepable(Player player) {
		PeakRoadInstance instance = player.getPeakRoadInstance();
		if (instance.getFloor() >= instance.getPassedMaxHardFloor()) {
			return false;
		}
		if (!instance.hasState(PeakRoadInstance.STATE_PREVIOUS_CHALLENGE_MASK) && PeakRoadUtils.getLeftTimes(player) <= 0) {
			return false;
		}
		return true;
	}
	
}
